<?php
    class CityController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
            $this->lng_id = '1';
           
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'city','default');
        }

        public function viewAction() {
            if ($this->_getParam('city_id') && $this->_getParam('city_id') != '') {
                $city_id = $this->_getParam('city_id');
            } else {
                $this->_helper->redirector('list', 'city', 'default');
            }
            $cityModel = new Model_City();
            $cityalldata = $cityModel->fetchEntryById($city_id);
            $this->view->cityalldata = $cityalldata;
        }
        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'lang_id';
            $sort = 'asc';
            $citysCount = array();


            /* Pagination Login */
            $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
               
                
            } 

            $cityModel = new Model_City();
            $citysData = $cityModel->fetchLISTCity($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $citysCount = $cityModel->fetchLISTCity($where);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal(count($citysCount));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'city', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->citysData = $citysData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $cityModel->getStatusArray();
            $this->view->searchtype = $searchtype;
        }
        public function deleteAction($ids = Array()) {
               //Load city Model 
               $cityModel = new Model_City();
               if ($this->_getParam('city_ref_id') && $this->_getParam('city_ref_id') != '') {
                   $city_ref_id = $this->_getParam('city_ref_id');
                   $cityModel->deleteCity($city_ref_id);
                   $this->addSuccessMessage('Record Successfully Deleted');
                  $this->_helper->redirector('list', 'city', 'default');
               } else {
                   $cityModel->deleteCity($ids);
                  $this->addSuccessMessage('Record Successfully Deleted');
                   $this->_helper->redirector('list', 'city', 'default');
               }
           }
        public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $cityData = Array();
            $this->view->mode = 'Add';

            //Load city Model
            $cityModel = new Model_City();

            //Load city Form
            $cityForm = new Form_CityForm();
            $countryModel = new Model_Country();
            $languageModel = new Model_Language();
//            $countryArray = $countryModel->fetchCountry();
            $countryArray = $countryModel->fetchCountry("country_lang_id = '" . $this->lng_id . "'");
            $languagesArray = $languageModel->fetchlanguages();
            $languages = Array();
            foreach ($languagesArray as $key => $_lan) {
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']]['city_identity_code'] = "";
                $languages[$_lan['lang_id']]['city_name'] = "";
                $languages[$_lan['lang_id']]['city_title'] = "";
                $languages[$_lan['lang_id']]['country_id'] = "";
                $languages[$_lan['lang_id']]['city_ref_id'] = "";
                $languages[$_lan['lang_id']]['city_id'] = "";
            }
            
            
            
            /*$languageArray = $languageModel->fetchlanguageArray();
            $cityForm->city_lang_id->addMultiOptions($languageArray);
            
            $countryModel = new Model_Country();
            $countryArray = $countryModel->fetchCountry();
            $cityForm->country_id->addMultiOptions($countryArray);
            
            //Load the status array for the select box in form 
            $statusArray = $cityModel->getStatusArray();
            $cityForm->city_status->addMultiOptions($statusArray);
*/
            
            //Check for the city Parameter
            $cityData['city_ref_id'] = $this->_request->getParam('city_ref_id');
            $city_ref_id = (!empty($cityData['city_ref_id'])) ? $cityData['city_ref_id'] : null;
            
            if (!empty($city_ref_id)) {
                //Set the Mode to Edit
                $this->view->mode = 'Edit';
                //Fetch data to be loaded in form 
                $data = $cityModel->fetchEntryByREFId($city_ref_id);
                foreach ($data as $_desc) {
                    if (array_key_exists($_desc['city_lang_id'], $languages)) {
                        $languages[$_desc['city_lang_id']]['city_identity_code'] = $_desc['city_identity_code'];
                        $languages[$_desc['city_lang_id']]['city_name'] = $_desc['city_name'];
                        $languages[$_desc['city_lang_id']]['city_title'] = $_desc['city_title'];
                        $languages[$_desc['city_lang_id']]['country_id'] = $_desc['country_id'];
                        $languages[$_desc['city_lang_id']]['city_id'] = $_desc['city_id'];
                        $languages[$_desc['city_lang_id']]['city_ref_id'] = $city_ref_id;
                    }
                }
            }
            //Populate form 
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
               /* if($postedData['city_identity_code']== ''){
                    $errorMessageArray[] = 'Please Enter identity code';
                }
                if($postedData['city_name']== ''){
                    $errorMessageArray[] = 'Please enter name';
                }*/
                if($postedData['country_id']== '' ){
                    $errorMessageArray[] = 'Please select country';
                }
              /*  $languages[1]['city_identity_code'] = $postedData['city_identity_code'];
                $languages[1]['city_name'] = $postedData['city_name'];
               */ $languages[1]['country_id'] = $postedData['country_id'];
                
                if($postedData['1_city_title']== ''){
                    $errorMessageArray[] = 'English value should not be blank.';
                    foreach ($languages as $key => $_lang) {
                      /*  $languages[$key]['city_identity_code'] = $postedData['city_identity_code'];
                        $languages[$key]['city_name'] = $postedData['city_name'];
                        */$languages[$key]['city_title'] = $postedData[$_lang['lang_id'].'_city_title'];
                        $languages[$key]['country_id'] = $postedData['country_id'];
                        
                    }
                }  
                    if (count($errorMessageArray) > 0) {

                    } else {
                        //city save
                       /* 
                        * 
                        * if ($this->view->mode == 'Add') {
                            $cityData['city_createddate'] = date('Y-m-d H:i:s');
                        }$cityData['city_id'] = $city_id;
                        $cityData['city_identity_code'] = $postedData['city_identity_code'];
                        $cityData['city_name'] = $postedData['city_name'];
                        $cityData['city_title'] = $postedData['city_title'];
                        $cityData['city_lang_id'] = $postedData['city_lang_id'];
                        $cityData['country_id'] = $postedData['country_id'];
                        $cityData['city_updateddate'] = date('Y-m-d H:i:s');
                        $cityData['city_status'] = $postedData['city_status'];
                        $cityModel->save($cityData, 'city_id');*/
                         if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code = $postedData[$_lang['lang_id'].'_city_title'];
                                $cityData['city_identity_code'] = strtolower($english_code);
                                $cityData['city_name'] = strtolower($english_code);
                                $cityData['city_title'] = $postedData[$_lang['lang_id'].'_city_title'];
                                $cityData['country_id'] = $postedData['country_id'];
                                $cityData['city_createddate'] = date('Y-m-d H:i:s');
                                $cityData['city_updateddate'] = date('Y-m-d H:i:s');
                                $cityData['city_lang_id'] = $_lang['lang_id'];
                                $cityData['city_status'] = 1;
                                $cityData['city_ref_id'] = $_lang['city_ref_id'];
                                $last_id = $cityModel->save($cityData, 'city_id');
                                $cityData1['city_id'] = $last_id;
                                $cityData1['city_ref_id'] = $last_id;
                                $cityModel->save($cityData1, 'city_id');
                            }else{
                                $cityData['city_identity_code'] = strtolower($english_code);
                                $cityData['city_name'] = strtolower($english_code);
                                $cityData['city_title'] = $postedData[$_lang['lang_id'].'_city_title'];
                                $cityData['country_id'] = $postedData['country_id'];
                                $cityData['city_createddate'] = date('Y-m-d H:i:s');
                                $cityData['city_updateddate'] = date('Y-m-d H:i:s');
                                $cityData['city_lang_id'] = $_lang['lang_id'];
                                $cityData['city_status'] = 1;
                                $cityData['city_ref_id'] = $last_id;
                                if($cityData['city_name']!= '' ){
                                    $cityModel->save($cityData, 'city_id');
                                }
                            }
                        }
                   }
                         else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $english_code = $postedData[$_lang['lang_id'].'_city_title'];
                                $cityData['city_id'] = $_lang['city_id'];
                                $cityData['city_identity_code'] = strtolower($english_code);
                                $cityData['city_name'] = strtolower($english_code);
                                $cityData['city_title'] = $postedData[$_lang['lang_id'].'_city_title'];
                                $cityData['country_id'] = $postedData['country_id'];
                                $cityData['city_createddate'] = date('Y-m-d H:i:s');
                                $cityData['city_updateddate'] = date('Y-m-d H:i:s');
                                $cityData['city_lang_id'] = $_lang['lang_id'];
                                $cityData['city_status'] = 1;
                                $cityData['city_updateddate'] = date('Y-m-d H:i:s');
                                $cityData['city_ref_id'] = $city_ref_id;
                                $cityModel->save($cityData, 'city_id');
                                
                            }else{
                                $cityData['city_id'] = $_lang['city_id'];
                                $cityData['city_identity_code'] = strtolower($english_code);
                                $cityData['city_name'] = strtolower($english_code);
                                $cityData['city_title'] = $postedData[$_lang['lang_id'].'_city_title'];
                                $cityData['country_id'] = $postedData['country_id'];
                                $cityData['city_createddate'] = date('Y-m-d H:i:s');
                                $cityData['city_updateddate'] = date('Y-m-d H:i:s');
                                $cityData['city_lang_id'] = $_lang['lang_id'];
                                $cityData['city_status'] = 1;
                                $cityData['city_updateddate'] = date('Y-m-d H:i:s');
                                $cityData['city_ref_id'] = $city_ref_id;
                                $cityModel->save($cityData, 'city_id');
                                
                            }
                        }
                     
                   }
                        
                        if (count($errorMessageArray) > 0) {

                        } else {
                            if ($this->view->mode == 'Add') {
                                $successmessage = 'City add successfully';
                            } else {
                                $successmessage = 'City updated successfully';
                            }
                        }
                        $this->addSuccessMessage($successmessage);
                        $this->_helper->redirector('list', 'city', 'default');
                    }
                
            }
            $this->view->countryArray = $countryArray;
            $this->view->languages = $languages;
            $this->view->cityid = $city_ref_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
           // $this->view->form = $cityForm;


    }
    }?>